package operator

import "github.com/spf13/viper"

type Config interface {
	Exsit(keys ...string) (unExsitKeys []string)
	IsExsit(keys ...string) bool
	GetString(key string) string
	GetStringWithDefault(key, vauleDefault string) string
	GetInt(key string) int
	GetInt32(key string) int32
	GetInt64(key string) int64
	GetFloat64(key string) float64
	GetIntWithDefault(key string, Default int) int
	GetBool(key string) bool
	Get(key string) interface{}
}


type ViperConfig struct {
	viperIns *viper.Viper
}

func NewViperConfig(params map[string]interface{}) *ViperConfig {
	c := &ViperConfig{
		viperIns: viper.New(),
	}
	if len(params) > 0 {
		for k, v := range params {
			c.viperIns.Set(k, v)
		}
	}
	return c
}

func (c *ViperConfig) Exsit(keys ...string) (unExsitKeys []string) {
	unExsitKeys = make([]string, 0)
	for _, k := range keys {
		v := c.viperIns.IsSet(k)
		if !v {
			unExsitKeys = append(unExsitKeys, k)
		}
	}
	return unExsitKeys
}

func (c *ViperConfig) IsExsit(keys ...string) bool {
	return len(c.Exsit(keys...)) == 0
}

func (c *ViperConfig) GetString(key string) string {
	return c.viperIns.GetString(key)
}
func (c *ViperConfig) GetStringWithDefault(key, vauleDefault string) string {
	value := c.viperIns.GetString(key)
	if c.viperIns.GetString(key) == "" {
		return vauleDefault
	}
	return value
}

func (c *ViperConfig) GetInt(key string) int {
	return c.viperIns.GetInt(key)
}
func (c *ViperConfig) GetInt32(key string) int32 {
	return c.viperIns.GetInt32(key)
}
func (c *ViperConfig) GetInt64(key string) int64 {
	return c.viperIns.GetInt64(key)
}
func (c *ViperConfig) GetFloat64(key string) float64 {
	return c.viperIns.GetFloat64(key)
}
func (c *ViperConfig) GetIntWithDefault(key string, Default int) int {
	result := c.viperIns.GetInt(key)
	if result == 0 {
		return Default
	}
	return result
}
func (c *ViperConfig) GetBool(key string) bool {
	return c.viperIns.GetBool(key)
}
func (c *ViperConfig) Get(key string) interface{} {
	return c.viperIns.Get(key)
}
